// proxyWithdrawDetail.js
const app = getApp()
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datetime:{
      startTime: util.formatdate(new Date((new Date().getFullYear()), (new Date().getMonth() - 6 ), (new Date().getDate()) ) ),
      endTime: util.formatdate(new Date()),
      startDate: new Date().getTime(),
      endDate: new Date().getTime(),
      timeshow:false,
      currentDate: new Date().getTime(),//1574646440123
      minDate: new Date((new Date().getFullYear() - 1), 0, 1).getTime(),
      maxDate: new Date((new Date().getFullYear() + 2), 11, 31).getTime(),
      timeStatus:0
    },
    list:[
      {
        month: '',
        amount: '',
        data:[
          {
            created:"",
            amount:''
          }
        ]
      }
    ]
  },
  // 确认时间
  timeConfirm(event) {
    var that = this;
    var date = new Date(event.detail);
    var time = util.formatdate(date);
    var datetime = that.data.datetime;
    var status = datetime.timeStatus;
    if(status == 0){
      that.setData({
        ['datetime.startTime']: time,
        ['datetime.startDate']: event.detail,
        ['datetime.timeshow']: false,
        ['datetime.minDate']: event.detail,
      });
      if(datetime.startDate>datetime.endDate){
        that.setData({
          ['datetime.endTime']: time,
          ['datetime.endDate']: event.detail,
        });
      }
    } else if (status == 1){
      that.setData({
        ['datetime.endTime']: time,
        ['datetime.endDate']: event.detail,
        ['datetime.timeshow']: false
      });
    }
  },
  // 显示时间选择
  timeShow(e) {
    var status = e.currentTarget.dataset.status;
    this.setData({ 
      ['datetime.timeshow']: true,
      ['datetime.timeStatus']: status,
    });
    if( status == 0 ){
      this.setData({
        ['datetime.minDate']: new Date((new Date().getFullYear() - 1), 0, 1).getTime(),
      })
    }
  },
  // 关闭时间选择
  timeClose() {
    this.setData({ ['datetime.timeshow']: false });
  },
  withdrawalsRecord:function(){
    var that = this;
    var data={
      beginDate: that.data.datetime.startTime,
      endDate: that.data.datetime.endTime
    };
    app.post('order/withdrawalsRecord', data).then((res) => {
      if (res.code == 200) {
        let dataList = res.list; //获取到的数据

        dataList.forEach((item) => {
          if(item.data){
            (item.data).forEach((item2) => {
              item2.created = item2.created.substring(0, 10); //要截取字段的字符串
            })
          }
          
        })
        that.setData({
          list: dataList
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.withdrawalsRecord();
    wx.stopPullDownRefresh()
  },
  timeSearch: function () {
    this.withdrawalsRecord();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 请求最新数据
    this.onLoad();
  }
})